<template>
	<view class="content">
		<uni-search-bar placeholder="请输入关键词搜索" class="uni-searchbar" cancelButton="none" radius="50" @confirm="search"
			@clear="clearKeyword" />
		<view v-if="blogs.length ==0" class="none">
			<image class="logo" mode="widthFix" src="/static///icons(1)/nodata.png"></image>
			<text>抱歉，数据加载不出来</text>
		</view>
		<view v-else class="item-container" v-for="(item,index) in blogs" :key="item.id" @click="gotoDetail(item.id)">
			<text class="item-title">{{item.title}}</text>
			<rich-text :nodes="item.description" class="item-desc"></rich-text>
			<image :src="item.picture" mode="widthFix" class="img item-pic"></image>
			
			<text class="iconfont icon-down share" @click.stop="popSheet"></text>
			
			<view>
				<text v-for="(tag,idx) in item.tags" :key="idx" class="item-tag">{{tag}}</text>
			</view>
			<image @click.stop="clickShowUser(item.user)" :src="item.user.avatar" mode="aspectFill" class="img-head"></image>
			<view class="icon-container">
				<image @click.stop="changeGood(item.id,true)" v-if="good_ids.indexOf(item.id) < 0"
					src="../../static/icons(1)/ic_good.png" class="icon"></image>
				<image @click.stop="changeGood(item.id,false)" v-else src="../../static/icons(1)/ic_good_fill.png"
					class="icon"></image>
				<text class="icon-text">{{item.good}}</text>
				<image src="../../static/icons(1)/ic_comment.png" class="icon"></image>
				<text class="icon-text">{{item.comments.length}}</text>
				<image src="../../static/icons(1)/ic_count.png" class="icon"></image>
				<text class="icon-text">{{item.readCount}}</text>
			</view>
		</view>
		<view class="buttom" v-if="blogs.length == count">
			-----我是有底线的-----
		</view>
		<dialog-shell ref="shell" title="用户描述" @confirm = "confirmInfo" >
			<text style="padding: 10rpx;font-size:28rpx ">{{userDesc}}</text>
		</dialog-shell>
	</view>
</template>

<script>
	let page = 0
	let size = 3
	let key_blogs = "blogs"
	export default {
		data() {
			return {
				blogs: [],
				count: -1,
				searching: false,
				good_ids: [],
				userDesc:""
			}
		},
		onLoad() {
			this.$noti.add(this.$params.noti_refresh_count,this.notifyRefreshCount,this)
			this.$noti.add(this.$noti_refresh_comment,this.notifyRefreshcomment,this)
			//缓存
			let ids = uni.getStorageSync(this.$params.key_good_ids)
			if (ids) {
				this.good_ids = ids
			}
			let blogs = uni.getStorageSync(key_blogs)
			if (blogs) {
				this.blogs = blogs
				this.count = blogs.length
			} else {
				this.getPagedBlogs()
			}
		},
		
		onUnload(){
			this.$noti.remove(this.$params.noti_refresh_count)//移除通知
		},
		onPullDownRefresh() {
			page = 0
			this.blogs = []
			this.count = -1
			this.getPagedBlogs()
		},
		onReachBottom() {
			if (this.searching || this.blogs.length == this.count) {
				return
			}
			page += 1
			this.getPagedBlogs()
		},
		methods: {
			notifyRefreshcomment(){
				
				this.blogs.forEach(b =>{
					if(blog.id ==info.id){
						blog.comments =info.comments
					}
				})
			},
			confirmInfo(){
				console.log("emit");
			},
			clickShowUser(user){
				this.userDesc = "作者：" + user.nickName + "\n联系方式：" + user.email
				this.$refs.shell.show()
			},
			gotoDetail(id) {
				uni.navigateTo({
					url: '../blog/blog?id=' + id,
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			},
			notifyRefreshCount(info){
				let id =info.id
				let count = info.count
				this.refreshReadCount(id,count)
			},
			refreshReadCount(id,count){
				console.log("刷新了列表页");
				this.blogs.forEach(b =>{
					if(b.id == id){
						b.readCount = count
					}
				})
			},
			changeGood(id, isGood) {
				let url =this.$params.host
				let action = isGood ? this.$params.action_good: this.$params.action_del_good
				url += action
				let data = {
					"id":id
				}
				this.$request.postParam(url, data ,res =>{
					this.blogs.forEach(blog =>{
						if(blog.id == id){
							console.log(blog);
							blog.good = res.data
						}
					})
					
					if (isGood) {
						this.good_ids = this.good_ids.concat(id)
					} else {
						let index = this.good_ids.indexOf(id)
						//shift删除第一个，pop删除最后一个，splice删除指定位置元素
						//pop 把最后一个元素替换要删除的元素，然后pop【1,2,3,】
						//数组较大且顺序不影响程序功能，可提高性能
						//this。good_ids.spilce(index，1)
						this.$util.remove(this.good_ids,index)
					}
					uni.setStorageSync(this.$params.key_good_ids, this.good_ids)//TODO:同步服务器，并且最新数据
					
					
				},()=>{})
			},
			getPagedBlogs() {
				console.log("开始分页请求");
				uni.setStorageSync("test", 123)
				if (this.blogs.length == this.count) {
					uni.showToast({
						title: '已经全部加载完成'
					});
					return
				}
				let header = {
					"content-type": "application/json;charset=UTF-8",
					"page": page,
					"size": size
				}

				this.$request.getWithHeader(this.$params.host + this.$params.action_blogs_page, header, res => {
					res.data.forEach(blog => {
						if (!blog.picture.startsWith("http")) {
							blog.picture = this.$params.host + blog.picture
						}
						blog.user.avatar = this.$params.host + blog.user.avatar
					})
					this.blogs = [...this.blogs, ...res.data]
					uni.setStorageSync(key_blogs, this.blogs)
					this.count = parseInt(res.message)
				}, () => {
					uni.stopPullDownRefresh()
				})
			},
			search(e) {
				let kw = e.value
				if (kw.length > 0) {
					// 1.清空列表数据
					//2.重置count
					//3.发起请求，要在常量文件中添加搜索的地址，使用get方法发起请求，请求的结果要处理
					this.blogs = []
					this.count = -1
					let url = this.$params.host + this.$params.action_search + kw
					this.$request.get(url, res => {
						res.data.forEach(blog => {
							if (!blog.picture.startsWith("http")) {
								blog.picture = this.$params.host + blog.picture
							}
							blog.user.avatar = this.$params.host + blog.user.avatar
						})
						this.blogs = [...this.blogs, ...res.data]
					}, () => {
						this.searching = true
					})
				}
			},
			clearKeyword() {
				this.blogs = []
				page = 0
				this.getPagedBlogs()
				this.searching = false
			},
			popSheet(id){
				uni.showActionSheet({
					itemList: ["删除","收藏"],
					success: res => {
						if(res.tapIndex == 0){
							uni.showModal({
								title: '删除blog',
								content: '确认删除吗？',
								showCancel: true,
								cancelText: '算了',
								confirmText: '删除',
								success: res => {
									if(res.confirm){
										this.deleteBlog(id)
									}
								},
								fail: () => {},
								complete: () => {}
							});
						}else{
							uni.showToast({
								title: '收藏成功'
							});
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			deleteBlog(id){
				uni.showToast({
					title: '还未登录，不能删除'
				});
			}
		}
	}
</script>

<style>
	@import url("index.css");
</style>